using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace DynamicDNS.Provider.Web
{
    /// <summary>
    /// Implementation for the webbased MyIP Dynamic DNS Provider
    /// </summary>
    public class MyIPProvider : DynamicDNS.Provider.BaseProvider.DynamicDNSWebBaseProvider, IDynamicDNSProvider
    {
        private const string URLTEMPLATE = "http://myip.us/cgi-bin/cgi_updateip?accountname={0}&password={1}&hostname={2}&domainname={3}&ipaddress={4}";

        public MyIPProvider()
        {
        }

        #region IDynamicDNSProvider Member

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourdomainname, string username, string password)
        {
            string[] domainsplit = yourdomainname.Split(',');
            if (domainsplit.Length<1)
            {
                throw new ArgumentException("The parameter yourdomain doesn't have the syntax 'subdomain,domainname'.");
            }
            return UpdateIP( ip,  domainsplit[0],domainsplit[1],  username,  password);
        }

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string subdomainname,string domainname, string username, string password)
        {
            BuildRequestString(ip, subdomainname, domainname, username, password);
            return base.UpdateDNSEntry();
        }


        /// <summary>
        /// This is a helper method for replacing the template values against the correct values
        /// </summary>
        /// <param name="ip">Contains the IP Adress for which the Update is to processed</param>
        /// <param name="subdomainname">Contains the Subdomain for which the Update is to processed. This is Provider specific.</param>
        /// <param name="domainname">Contains the Domain for which the Update is to processed. This is Provider specific.</param>
        /// <param name="username">Contains the required username for the update</param>
        /// <param name="password">Contains the required password for the update</param>
        private void BuildRequestString(IPAddress ip, string subdomainname,string domainname, string username, string password)
        {
            Url = String.Format(URLTEMPLATE, username, password, subdomainname, domainname, ip.ToString());
        }

        /// <summary>
        /// This method is overwritten for provider specific processing
        /// </summary>
        /// <param name="response">Contains the Response as a string provided by the Webserver</param>
        /// <returns>DynamicDNSUpdateResult indicates, whether an Update was successfull or not.</returns>
        protected override DynamicDNSUpdateResult CheckResponse(string response)
        {
            switch (response.Substring(0, response.IndexOf(" ")).ToLower())
            {
                case "1015":
                    // The update was successful, and the hostname is now updated
                    // Check http://www.myip.us/specifications.html for further response codes
                    return  new DynamicDNSUpdateResult(DynamicDNSStatus.OK, "");
                     
                default:
                    return  new DynamicDNSUpdateResult(DynamicDNSStatus.Error, "Error Code: " + response.Substring(0, response.IndexOf(" ")).ToLower());
                     
            }
        }

        #endregion
    }
}
